package servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import dao.AdminDao;
import entity.Admin;
import entity.AdminPrivs;

public class QueryAdminPrivsServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4062895450399891277L;
	private List<Map<String, String>> list = null;
	private String adminName;
	private String deptName;
	private String priv;
	private String status;
	
	public QueryAdminPrivsServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		// PARAMS
		adminName = request.getParameter("adminName");
		deptName = request.getParameter("deptName");
		priv = request.getParameter("priv");
		status = request.getParameter("status");
		
		List<Admin> admins = null;
		AdminDao adminDao = null;
		AdminPrivs adminPrivs = null;
		StringBuffer privsJson = null;
		Set<String> privsSet = null;
		
		try {
			adminDao = new AdminDao();
			setCondition();
			admins = adminDao.queryAdmins(list);
			privsJson = new StringBuffer();
			if(admins != null && admins.size() > 0) {
				privsJson.append("{\"retCode\":\"1\",");
				privsJson.append("\"adminPrivs\":[");
				for(Admin admin : admins) {// 一个admin一条记录
					// adminPrivs = new AdminPrivs();
					adminPrivs = adminDao.queryAdminPrivs(admin.getAdminName());
					privsSet = adminPrivs.getPrivNameAddUser().keySet();
					privsJson.append("{");
					privsJson.append("\"adminName\":\"").append(admin.getAdminName()).append("\",");
					privsJson.append("\"privs\":\"");
					
					if (privsSet.size() > 0) {
						for(String priv : privsSet) {// 一条记录多个权限
							privsJson.append(priv).append("、");
						}
						privsJson.deleteCharAt(privsJson.length() - 1);
					}
					
					privsJson.append("\",");
					privsJson.append("\"status\":\"").append(admin.getStatus()).append("\",");
					privsJson.append("\"deptName\":\"").append(admin.getDeptName()).append("\",");
					privsJson.append("\"phone\":\"").append(admin.getPhone()).append("\",");
					privsJson.append("\"isSuper\":\"").append(admin.getIsSuper()).append("\",");
					privsJson.append("\"password\":\"").append(admin.getPassword()).append("\",");
					privsJson.append("\"email\":\"").append(admin.getEmail()).append("\",");
					privsJson.append("\"cnName\":\"").append(admin.getCnName()).append("\",");
					privsJson.append("\"createDate\": \"").append(admin.getCreateDate().toString()).append("\",");
					privsJson.append("\"approveNum\":\"").append(admin.getApproveNum()).append("\"");
					privsJson.append("},");
				}
				privsJson.deleteCharAt(privsJson.length() - 1).append("]}");
			} else {
				privsJson.append("{\"retCode\":\"0\",");
				privsJson.append("\"desc\":\"QueryAdminPrivsServlet 无记录\"}");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			privsJson.append("{\"retCode:\"\"0\",\"desc\":\"QueryAdminPrivsServlet Exception\"}");
		} finally {
			out.print(privsJson.toString());
			out.close();
		}
	}

	// 封装查询条件
	private void setCondition() throws UnsupportedEncodingException {
		list = new ArrayList<Map<String, String>>();
		if(!StringUtils.isEmpty(adminName)) {
			adminName = URLDecoder.decode(URLDecoder.decode(adminName, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "adminName");
			map.put("值", adminName);
			list.add(map);
		}
		if(!StringUtils.isEmpty(deptName)) {
			deptName = URLDecoder.decode(URLDecoder.decode(deptName, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "deptName");
			map.put("值", deptName);
			list.add(map);
		}
		if(!StringUtils.isEmpty(priv)) {
			priv = URLDecoder.decode(URLDecoder.decode(priv, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "privName");
			map.put("值", priv);
			list.add(map);
		}
		if(!StringUtils.isEmpty(status)) {
			status = URLDecoder.decode(URLDecoder.decode(status, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "status");
			map.put("值", status);
			list.add(map);
		}
	}
	
	public void init() throws ServletException {
	}

}
